<?PHP
/**
 * User: Paulo Fournier
 * Date: 19-08-2013
 * Type: class
 * Google latLong object
 */

class LatLong
{

    /***
     * Constructor
     * @param $lat In decimal degrees
     * @param $long In decimal degrees
     */
    function __construct($lat, $long)
    {
        $this->lat = $lat * M_PI / 180;
        $this->long = $long * M_PI / 180;
        $this->coords = Array('lat' => $lat, 'long' => $long);
    }

    //
    /***
     * 1: Calculate distance (in km)
     * Between two points specified by latitude/longitude with Haversine formula
     * @param $p1 LatLong object
     * @param $p2 LatLong object
     * @return float Distance is km
     */
    public static function distHaversine($p1, $p2)
    {
        $R = EARTH_MEAN_RADIUS;
        $dLat = $p2->lat - $p1->lat;
        $dLong = $p2->long - $p1->long;

        $a = sin($dLat / 2) * sin($dLat / 2) +
            cos($p1->lat) * cos($p2->lat) * sin($dLong / 2) * sin($dLong / 2);
        $c = 2 * atan2(sqrt($a), sqrt(1 - $a));
        $d = $R * $c;

        return $d;
    }

    //
    /***
     * 2: Calculate distance (in km)
     * Between two points specified by latitude/longitude using law of cosines.
     * @param $p1
     * @param $p2
     * @return float
     */
    public static function distCosineLaw($p1, $p2)
    {
        $d = acos(sin($p1->lat) * sin($p2->lat) + cos($p1->lat) * cos($p2->lat) * cos($p2->long - $p1->long)) * EARTH_MEAN_RADIUS;
        return $d;
    }
}